package org.metasketch.ham.diagrams;

import org.metasketch.diagram.javafx.graphics.BaseGraphic;
import org.metasketch.diagram.javafx.graphics.CustomLabel;
import org.metasketch.diagram.javafx.graphics.Resizableness;
import org.metasketch.editor.toolbox.IPointCalculator;
import org.metasketch.graphics.GraphicNode;
import org.metasketch.graphics.Point;
import org.metasketch.ham.diagrams.units.ActorNode;
import org.metasketch.ham.diagrams.units.ArtifactNode;
import org.metasketch.ham.diagrams.units.ParticipationUnitNode;
import org.metasketch.ham.diagrams.units.RoleNode;
import org.metasketch.ham.diagrams.units.SystemNode;
import org.metasketch.ham.model.HumanParticipationUnit;
import org.metasketch.ham.model.HumanParticipationUnitType;

public class HumanParticipationUnitNode extends BaseGraphic implements
		IPointCalculator {

	ParticipationUnitNode unitNode;

	CustomLabel nameLabel = new CustomLabel("Untitled");

	public HumanParticipationUnitNode() {
		super(new GraphicNode(0, 0, 60, 70));

		setResizable(Resizableness.HORIZONTAL);
	}

	@Override
	protected void continueSetup() {

		setupGraphic(graphicNode.getX(), graphicNode.getY(),
				graphicNode.getWidth(), graphicNode.getHeight());

	}

	private void setupGraphic(int x, int y, int width, int height) {

		this.width = width;
		this.height = height;

		setX(x);
		setY(y);

		nameLabel.setGraphic(this);
		nameLabel.setVerticallyCentered(false);
		nameLabel.setFieldNameEditing("name", true);
		nameLabel.setTopPadding("" + (height - 20));

		getChildren().addAll(nameLabel);

		// if (unitNode != null) {
		// getChildren().addAll(unitNode);
		// }

	}

	@Override
	protected void resizeGraphic() {

		if (unitNode != null) {

			// int transX = width / 2 - 20;

			// unitNode.setTranslateX(transX);
			// unitNode.setTranslateY(0);

			unitNode.setTranslateX(width * 0.5 - unitNode.getWidth() * 0.5);
			unitNode.setTranslateY(0);

		}

		nameLabel.resize();

	}

	@Override
	protected void refreshModelGraphic() {
		HumanParticipationUnit unit = (HumanParticipationUnit) semantic;

		getChildren().remove(unitNode);

		HumanParticipationUnitType type = unit.getType();
		if (type == HumanParticipationUnitType.ROLE) {
			unitNode = new RoleNode();
		} else if (type == HumanParticipationUnitType.ARTIFACT) {
			unitNode = new ArtifactNode();
		} else if (type == HumanParticipationUnitType.SYSTEM) {
			unitNode = new SystemNode();
		} else if (type == HumanParticipationUnitType.ACTOR) {
			unitNode = new ActorNode();
		}

		if (unitNode != null) {
			getChildren().add(0, unitNode);

			resizeGraphic();
		}

		nameLabel.refreshModelGraphic();

	}

	@Override
	public Point getClosestPoint(int x, int y) {

		Point coords = getCompleteCoordinatesThroughParent();

		if (x > coords.getX() + unitNode.getTranslateX() + unitNode.getWidth()
				+ 10)
			x = (int) (coords.getX() + unitNode.getTranslateX()
					+ unitNode.getWidth() + 10);
		else if (x < coords.getX() + unitNode.getTranslateX() - 10)
			x = (int) (coords.getX() + unitNode.getTranslateX() - 10);

		if (y > coords.getY() + unitNode.getTranslateY() + unitNode.getHeight()
				+ 20)
			y = (int) (coords.getY() + unitNode.getTranslateY()
					+ unitNode.getHeight() + 20);
		else if (y < coords.getY() + unitNode.getTranslateY() - 10)
			y = (int) (coords.getY() + unitNode.getTranslateY() - 10);

		// point.setX((int) x);
		// point.setY((int) y);

		return new Point((int) x, (int) y);
	}

	@Override
	public Point getCenter() {

		Point coords = getCompleteCoordinatesThroughParent();

		return new Point((int) (coords.getX() + unitNode.getTranslateX() + 10),
				(int) (coords.getY() + unitNode.getTranslateY() + 30));
	}

}
